package app.server.project.main.service.db.dao.cfg;

import app.server.project.main.service.db.entity.cfg.RaceCfgEntity;
import app.server.project.main.service.db.mapper.cfg.RaceCfgMapper;
import app.server.project.main.service.service.race.CoinType;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RaceCfgDao extends ServiceImpl<RaceCfgMapper, RaceCfgEntity> {


    private List<RaceCfgEntity> cfgEntityList = null;

    public List<RaceCfgEntity> getAll() {
        return cfgEntityList;
    }

    @PostConstruct
    public void init() {
        this.flush();
    }

    public void flush() {
        this.cfgEntityList = this.list(Wrappers.<RaceCfgEntity>lambdaQuery().orderByAsc(RaceCfgEntity::getOrderIndex));
    }

    public RaceCfgEntity getOne(int raceId) {
        return this.cfgEntityList.stream().filter(v -> v.getId() == raceId).findFirst().get();
    }

    public RaceCfgEntity getAiCoin() {
        return this.getOne(CoinType.AI_COIN);
    }

    public void del(int raceId) {
        this.removeById(raceId);
    }


}
